package com.localjoycommunity.cpdj.project.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.localjoycommunity.cpdj.common.utils.DateUtils;
import com.localjoycommunity.cpdj.project.system.domain.Building;
import com.localjoycommunity.cpdj.project.system.mapper.BuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.localjoycommunity.cpdj.project.system.mapper.TwoNewOrganizationMapper;
import com.localjoycommunity.cpdj.project.system.domain.TwoNewOrganization;
import com.localjoycommunity.cpdj.project.system.service.ITwoNewOrganizationService;

/**
 * 两新组织Service业务层处理
 *
 * @author leepandar
 * @date 2020-11-04
 */
@Service
public class TwoNewOrganizationServiceImpl implements ITwoNewOrganizationService {
    @Autowired
    private TwoNewOrganizationMapper twoNewOrganizationMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    /**
     * 查询两新组织
     *
     * @param id 两新组织ID
     * @return 两新组织
     */
    @Override
    public TwoNewOrganization selectTwoNewOrganizationById(Long id) {
        TwoNewOrganization twoNewOrganization = twoNewOrganizationMapper.selectTwoNewOrganizationById(id);
        String imgs = twoNewOrganization.getImgs();
        if (!"".equals(imgs) && imgs != null) {
            List imgList = Arrays.asList(imgs.split(","));
            twoNewOrganization.getImages().addAll(imgList);
        }
        return twoNewOrganization;
    }

    /**
     * 查询两新组织列表
     *
     * @param twoNewOrganization 两新组织
     * @return 两新组织
     */
    @Override
    public List<TwoNewOrganization> selectTwoNewOrganizationList(TwoNewOrganization twoNewOrganization) {
        return twoNewOrganizationMapper.selectTwoNewOrganizationList(twoNewOrganization);
    }

    /**
     * 新增两新组织
     *
     * @param twoNewOrganization 两新组织
     * @return 结果
     */
    @Override
    public int insertTwoNewOrganization(TwoNewOrganization twoNewOrganization) {
        twoNewOrganization.setCreateTime(DateUtils.getNowDate());
        String buildingName = twoNewOrganization.getBuildingName();
        Building building = new Building();
        building.setBuildingName(buildingName);
        List<Building> list = buildingMapper.selectBuildingList(building);
        if (list != null && list.size() > 0) {
            building = list.get(0);
            twoNewOrganization.setBuildingId(building.getBuildingId());
        }
        String logo = twoNewOrganization.getLogo();
        if ("".equals(logo) || logo == null) {
            twoNewOrganization.setLogo("http://image.localjoycommunity.com/logo.png");
        }
        return twoNewOrganizationMapper.insertTwoNewOrganization(twoNewOrganization);
    }

    /**
     * 修改两新组织
     *
     * @param twoNewOrganization 两新组织
     * @return 结果
     */
    @Override
    public int updateTwoNewOrganization(TwoNewOrganization twoNewOrganization) {
        String buildingName = twoNewOrganization.getBuildingName();
        Building building = new Building();
        building.setBuildingName(buildingName);
        List<Building> list = buildingMapper.selectBuildingList(building);
        if (list != null && list.size() > 0) {
            building = list.get(0);
            twoNewOrganization.setBuildingId(building.getBuildingId());
        }
        return twoNewOrganizationMapper.updateTwoNewOrganization(twoNewOrganization);
    }

    /**
     * 批量删除两新组织
     *
     * @param ids 需要删除的两新组织ID
     * @return 结果
     */
    @Override
    public int deleteTwoNewOrganizationByIds(Long[] ids) {
        return twoNewOrganizationMapper.deleteTwoNewOrganizationByIds(ids);
    }

    /**
     * 删除两新组织信息
     *
     * @param id 两新组织ID
     * @return 结果
     */
    @Override
    public int deleteTwoNewOrganizationById(Long id) {
        return twoNewOrganizationMapper.deleteTwoNewOrganizationById(id);
    }

    @Override
    public int updateDigNum(long id, int num) {
        return twoNewOrganizationMapper.updateDigNum(id, num);
    }
}
